
package backupUtility;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.table.DefaultTableModel;
/**
 * This class contains the graphical user interface for the FileListGetter.java   
 * @author Changcoco Kevin Paolo
 * @author Mamis Dominique Bernard
 * @author Palasi Jandy
 * @author Rafael Ian Paolo 
 */
public class FileListUI  extends JFrame implements ActionListener{
    private JPanel scrollPanel;
    private JPanel buttonPanel;
    private JScrollPane scrollPane;
    private JTable choicesTable;
    private FileListGetter getList;
    private JButton saveButton;
    private JButton cancelButton;
    private JLabel chooseLabel;
    private ArrayList<File> arrayListofFiles;
    
    public FileListUI(){
        getList = new FileListGetter();
        arrayListofFiles = getList.getArrayListofFiles();
        Object [] columnNames = {"File Name", ""};
        Object [][] fileNames = new Object[arrayListofFiles.size()][2];
        for(int i = 0; i < fileNames.length; i++){
            fileNames[i][0] = arrayListofFiles.get(i).getName();
            fileNames[i][1] = false;
        }
        
        DefaultTableModel model = new DefaultTableModel(fileNames, columnNames);
        choicesTable = new JTable(model) {
            public Class getColumnClass(int column) {
                switch (column) {
                    case 0:
                        return String.class;
                    default:
                        return Boolean.class;
                }
            }
        
           public boolean isCellEditable(int row, int column) {
            return column == 1;
            }
        };
        choicesTable.setPreferredScrollableViewportSize(choicesTable.getPreferredSize());
        choicesTable.getColumnModel().getColumn(0).setPreferredWidth(680);
        choicesTable.getColumnModel().getColumn(1).setPreferredWidth(20);
        
        scrollPane = new JScrollPane(choicesTable);
        scrollPane.setPreferredSize(new Dimension(700,400));
        
        saveButton = new JButton("Backup Files!");
        saveButton.addActionListener(this);
        
        cancelButton = new JButton("Cancel");
        cancelButton.addActionListener(this);
        
        chooseLabel = new JLabel("Select Files to Backup: ");
      
        scrollPanel = new JPanel();
        scrollPanel.setLayout(new BoxLayout(scrollPanel, BoxLayout.PAGE_AXIS));
        scrollPanel.add(chooseLabel);
        scrollPanel.add(Box.createRigidArea(new Dimension(0,5)));
        scrollPanel.add(scrollPane);
        scrollPanel.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));

        buttonPanel = new JPanel();
        buttonPanel.setLayout(new BoxLayout(buttonPanel, BoxLayout.LINE_AXIS));
        buttonPanel.setBorder(BorderFactory.createEmptyBorder(0, 10, 10, 10));
        buttonPanel.add(Box.createHorizontalGlue());
        buttonPanel.add(saveButton);
        buttonPanel.add(Box.createRigidArea(new Dimension(10, 0)));
        buttonPanel.add(cancelButton);
        
        Container contentPane = getContentPane();
        contentPane.add(scrollPanel, BorderLayout.CENTER);
        contentPane.add(buttonPanel, BorderLayout.PAGE_END);

        pack();
        setVisible(true);
        setSize(800,500);
        setResizable(false);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
    
    public void actionPerformed(ActionEvent ae){
        if(ae.getSource()==saveButton){
        ArrayList<File> selectedFiles = new ArrayList<File>();
        for(int i = 0; i < choicesTable.getRowCount(); i++){
            if(choicesTable.getModel().getValueAt(i,1) == true){
               // System.out.println(arrayListofFiles.get(i).getName());
                selectedFiles.add(arrayListofFiles.get(i));
            }
            
            
        }
            UserInput bu = new UserInput(selectedFiles);
            bu.show();
            dispose();    

        }else if(ae.getSource()==cancelButton){
//            BackupUtilityUI reset = new BackupUtilityUI();
//            String[] args = new String[0];
//            reset.main(args);
            BackupUtilityUI bu = new BackupUtilityUI();
            bu.show();
            dispose();
        }
    
    }
}
